
// export const state = () => {
//   return {

//   }
// }

// 上面缩写

export const state = () => ({

  userinfo: {}

})

export const mutations = {
  setUserInfo(state, data) {
    state.userinfo = data
  },
  // 清除用户数据
  remove(state, info) {
    // if (process, browser) {
    localStorage.removeItem('userinfo')

    state.userinfo = {}
  }
}


export const getters = {
  username(state) {
    if (state.userinfo.user) {
      return state.userinfo.user.nickname
    } else {
      return '等待数据'
    }
  }
}


// 如果我们需要在 vuex 里面封装异步操作，就要经过 actions 
// 调用异步的操作以后才能用  mutations 来改变数据
export const actions = {
  login(srore, data) {
    this.$axios({
      method: 'post',
      url: '/accounts/login',
      data
    }).then(res => {
      srore.commit('setUserInfo', res.data)
    })
  }
}